{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23:59:30\n",
      "23:59:31\n",
      "23:59:32\n",
      "23:59:33\n",
      "23:59:34\n",
      "23:59:35\n",
      "23:59:36\n",
      "23:59:37\n",
      "23:59:38\n"
     ]
    }
   ],
   "source": [
    "class Clock(object):\n",
    "\n",
    "    def __init__(self, hour=0, minute=0, second=0):\n",
    "        self._hour = hour\n",
    "        self._minute = minute\n",
    "        self._second = second\n",
    "\n",
    "    def run(self):\n",
    "        self._second += 1\n",
    "        if self._second >= 60:\n",
    "            self._minute += 1\n",
    "            self._second -= 60\n",
    "        elif self._minute >= 60:\n",
    "            self._hour += 1\n",
    "            self._minute -= 60\n",
    "        elif self._hour >= 24:\n",
    "            self._hour -= 24\n",
    "\n",
    "    def show(self):\n",
    "        return '%02d:%02d:%02d' % \\\n",
    "               (self._hour, self._minute, self._second)\n",
    "def main():\n",
    "    clock = Clock(23, 59, 30)\n",
    "    times = 1\n",
    "    while True:\n",
    "        print(clock.show())\n",
    "        clock.run()\n",
    "        times += 1\n",
    "        if times == 10:\n",
    "            break\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 5)\n",
      "(7, 7)\n",
      "(6, 9)\n",
      "5.0\n"
     ]
    }
   ],
   "source": [
    "#完成定义一个类描述平面上的点并提供移动点和计算到另一个点距离的方法。\n",
    "from math import sqrt\n",
    "\n",
    "\n",
    "class Point(object):\n",
    "\n",
    "    def __init__(self, x=0, y=0):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "    def move_to(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "\n",
    "    def move_by(self, dx, dy):\n",
    "        self.x += dx\n",
    "        self.y += dy\n",
    "\n",
    "    def distance_to(self, other):\n",
    "        d1 = abs(other.x-self.x)\n",
    "        d2 = abs(other.y-self.y)\n",
    "        \n",
    "        return sqrt(pow(d1,2)+pow(d2,2))\n",
    "\n",
    "    def __str__(self):\n",
    "        return '(%s, %s)' % (str(self.x), str(self.y))\n",
    "\n",
    "\n",
    "def main():\n",
    "    p1 = Point(3, 5)\n",
    "    p2 = Point(7, 7)\n",
    "    print(p1)\n",
    "    print(p2)\n",
    "    p2.move_by(-1, 2)\n",
    "    print(p2)\n",
    "    print(p1.distance_to(p2))\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "570feb405e2e27c949193ac68f46852414290d515b0ba6e5d90d076ed2284471"
  },
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
